package com.power.sifa.statistics.model.vo;

import cn.hutool.core.collection.CollectionUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName BaseGraphVO
 * @Author xiongzw
 * @Date 2023/11/3 17:35
 * @Description 基本柱状统计图VO
 * @Version: 1.0
 **/

@Data
@AllArgsConstructor
@Builder
public class BaseGraphVO {

    /**
     * 条形统计图的分类
     */
    private List<String> columnList;

    /**
     * 条形统计图的对应值
     */
    private List<Long> valueList;


    public BaseGraphVO() {
        this.columnList = new ArrayList<>();
        this.valueList = new ArrayList<>();
    }


    public void sort() {
        if (CollectionUtil.isNotEmpty(this.columnList)) {
            List<Map<String,Object>> sortMapList = new ArrayList<>();
            for (int index = 0; index < this.columnList.size(); index++) {
                Map<String,Object> valueMap = new HashMap<>();
                valueMap.put("name", this.columnList.get(index));
                if (index < this.valueList.size()) {
                    valueMap.put("value",this.valueList.get(index));
                } else {
                    valueMap.put("value",0L);
                }
                sortMapList.add(valueMap);
            }
            // 进行排序
            sortMapList = sortMapList.stream().sorted(Comparator.comparing(el -> ((Long) ((Map<String, Object>) el).get("value"))).reversed()).collect(Collectors.toList());

            this.columnList = sortMapList.stream().map(el -> (String) el.get("name")).collect(Collectors.toList());
            this.valueList = sortMapList.stream().map(el -> (Long) el.get("value")).collect(Collectors.toList());
        }

    }


}
